//
//  NewTaskViewController.m
//  IPTV
//
//  Created by liao Echo on 14-5-17.
//  Copyright (c) 2014年 Echo. All rights reserved.
//

#import "NewTaskViewController.h"
#import "PokerControlClient.h"

@interface NewTaskViewController ()

@end

@implementation NewTaskViewController
@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)topBtnPressed:(UIButton*)btn{
    btn.selected = !btn.selected;
    topSelect = btn.selected;
    [self setTopState];
}
-(void)bottomBtnPressed:(UIButton*)btn{
    btn.selected = !btn.selected;
    bottomSelect = btn.selected;
    [self setBottomState];
}
-(void)save{
    SocketInfo *socketinfo = [[SocketInfo alloc] init];
    socketinfo.type = totalIndex;
    socketinfo.monday_able = xingqi1;
    socketinfo.tuesday_able = xingqi2;
    socketinfo.wednesday_able = xingqi3;
    socketinfo.thursday_able = xingqi4;
    socketinfo.friday_able = xingqi5;
    socketinfo.saturday_able = xingqi6;
    socketinfo.sunday_able = xingqi7;
    if (totalIndex == 0) {//定时
        NSString *str = [NSString stringWithFormat:@"%02d:%02d", topHours, topMinutes];
        socketinfo.time = str;
        if (topSelect) {
            socketinfo.onState = YES;
            [[PokerControlClient share] startDelayOnOff:@"15" code:@"1" time:str];
        }else{
            [[PokerControlClient share] startDelayOnOff:@"15" code:@"0" time:str];
        }
    }else if (totalIndex == 1) {//延时
        NSString *str = [NSString stringWithFormat:@"%02d:%02d", bottomHours, bottomMinutes];
        socketinfo.time = str;
        if (bottomSelect) {
            socketinfo.onState = YES;
            [[PokerControlClient share] startDelayOnOff:@"17" code:@"1" time:str];
        }else{
            [[PokerControlClient share] startDelayOnOff:@"17" code:@"0" time:str];
        }
    }
    [delegate newTaskType:socketinfo];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem textBarButtonItem:STR(@"保存") target:self action:@selector(save)];
    
    int width = 150;
    int height = 70;
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 40)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    
    UIImageView *imagelogo = [[UIImageView alloc] initWithImage:IMG(@"smile.png")];
    imagelogo.frame = CGRectMake(FX(topView)+FW(topView), 7, 25, 25);
    [self.view addSubview:imagelogo];
    
    labelDesc = [GoodLabel labelWithFont:12 title:@""];
    labelDesc.textColor = [UIColor blackColor];
    labelDesc.frame = CGRectMake(FX(topView)+FW(topView)+30+2, 0, SCREEN_WIDTH-width-32, FH(topView));
    [self.view addSubview:labelDesc];
    
    GoodBtn *btn = [GoodBtn btnWithImg:@"socket_status_off.png"];
    [btn setBackgroundImage:IMG(@"socket_status_on.png") forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(topBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(FW(topView)-80, 5, 80, 30);
    [topView addSubview:btn];
    
    GoodLabel *label = [GoodLabel labelWithFont:13 title:STR(@"定时开/关")];
    if ([Global curLanguageEn]) {
        label.text = @"on/off";
    }
    label.textColor = [UIColor blackColor];
    label.frame = CGRectMake(10, 0, 100, FH(topView));
    [topView addSubview:label];
    
    picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, width, height)];
    picker.backgroundColor = [UIColor grayColor];
    picker.delegate = self;
    picker.dataSource = self;
    picker.showsSelectionIndicator = YES;
    [self.view addSubview:picker];
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, FY(picker)+FH(picker), width, 40)];
    bottomView.backgroundColor = topView.backgroundColor;
    [self.view addSubview:bottomView];
    
    btn = [GoodBtn btnWithImg:@"socket_status_off.png"];
    [btn setBackgroundImage:IMG(@"socket_status_on.png") forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(bottomBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(FW(topView)-80, 5, 80, 30);
    [bottomView addSubview:btn];
    
    label = [GoodLabel labelWithFont:13 title:STR(@"延时开/关")];
    label.textColor = [UIColor blackColor];
    label.frame = CGRectMake(10, 0, 100, FH(topView));
    if ([Global curLanguageEn]) {
        label.frame = CGRectMake(0, 0, 100, FH(topView));
        label.text = @"delay on/off";
    }
    [bottomView addSubview:label];
    
    picker1 = [[UIPickerView alloc] initWithFrame:CGRectMake(0, FY(bottomView)+FH(bottomView), width, height)];
    picker1.backgroundColor = [UIColor grayColor];
    picker1.delegate = self;
    picker1.dataSource = self;
    picker1.showsSelectionIndicator = YES;
    [self.view addSubview:picker1];
    
    
    int btnWidth = 30;
    
    for (int i=0; i<3; i++) {
        for (int j=0; j<3; j++) {
            GoodBtn *btnXingQi = [GoodBtn btnWithImg:@"device_closed"];
            [btnXingQi setBackgroundImage:IMG(@"device_opened.png") forState:UIControlStateSelected];
            [btnXingQi addTarget:self action:@selector(xingqiBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
            btnXingQi.frame = CGRectMake(FX(topView)+FW(topView)+10+j*(btnWidth+20), FY(topView)+FH(topView)+10+i*(btnWidth+15), btnWidth, btnWidth);
            [self.view addSubview:btnXingQi];
            
            GoodLabel *label = [GoodLabel labelWithFont:12 title:@""];
            int index = j+i*3+1;
            if (index == 1) {
                label.text = @"一";
                if ([Global curLanguageEn]) {
                    label.text = @"M";
                }
            }else if (index == 2) {
                label.text = @"二";
                if ([Global curLanguageEn]) {
                    label.text = @"T";
                }
            }else if (index == 3) {
                label.text = @"三";
                if ([Global curLanguageEn]) {
                    label.text = @"W";
                }
            }else if (index == 4) {
                label.text = @"四";
                if ([Global curLanguageEn]) {
                    label.text = @"T";
                }
            }else if (index == 5) {
                label.text = @"五";
                if ([Global curLanguageEn]) {
                    label.text = @"F";
                }
            }else if (index == 6) {
                label.text = @"六";
                if ([Global curLanguageEn]) {
                    label.text = @"S";
                }
            }else if (index == 7) {
                label.text = @"日";
                if ([Global curLanguageEn]) {
                    label.text = @"S";
                }
            }
            if (index > 7) {
                btnXingQi.hidden = YES;
            }
            btnXingQi.tag = index;
            label.frame = CGRectMake(FX(btnXingQi)+FW(btnXingQi)+5, FY(btnXingQi), 10, FH(btnXingQi));
            [self.view addSubview:label];
        }
    }
}
-(void)xingqiBtnPressed:(UIButton*)btn{
    btn.selected = !btn.selected;
    if (btn.tag == 1) {
        xingqi1 = btn.selected;
    }else if (btn.tag == 2) {
        xingqi2 = btn.selected;
    }else if (btn.tag == 3) {
        xingqi3 = btn.selected;
    }else if (btn.tag == 4) {
        xingqi4 = btn.selected;
    }else if (btn.tag == 5) {
        xingqi5 = btn.selected;
    }else if (btn.tag == 6) {
        xingqi6 = btn.selected;
    }else if (btn.tag == 7) {
        xingqi7 = btn.selected;
    }
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return 24;
    }
    return 60;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

#pragma UIPickerView Delegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [NSString stringWithFormat:@"%d", row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (pickerView == picker) {
        totalIndex = 0;
        if (component == 0) {
            topHours = row;
        }else{
            topMinutes = row;
        }
        [self setTopState];
    }else{
        totalIndex = 1;
        if (component == 0) {
            bottomHours = row;
        }else{
            bottomMinutes = row;
        }
        [self setBottomState];
    }
}
-(void)setTopState{
    if (topSelect) {
        labelDesc.text = [NSString stringWithFormat:STR(@"插座设置为%d点%02d分打开"), topHours, topMinutes];
        if ([Global curLanguageEn]) {
            labelDesc.text = [NSString stringWithFormat:STR(@"%d:%02d on"), topHours, topMinutes];
        }
    }else{
        labelDesc.text = [NSString stringWithFormat:STR(@"插座设置为%d点%02d分关闭"), topHours, topMinutes];
        if ([Global curLanguageEn]) {
            labelDesc.text = [NSString stringWithFormat:STR(@"%d:%02d off"), topHours, topMinutes];
        }
    }
}
-(void)setBottomState{
    if (bottomSelect) {
        labelDesc.text = [NSString stringWithFormat:STR(@"插座设置为%d时%d分后打开"), bottomHours, bottomMinutes];
        if ([Global curLanguageEn]) {
            labelDesc.text = [NSString stringWithFormat:STR(@"%d hours %d mins on"), bottomHours, bottomMinutes];
        }
    }else{
        labelDesc.text = [NSString stringWithFormat:STR(@"插座设置为%d时%d分后关闭"), bottomHours, bottomMinutes];
        if ([Global curLanguageEn]) {
            labelDesc.text = [NSString stringWithFormat:STR(@"%d hours %d mins off"), bottomHours, bottomMinutes];
        }
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
